package com.example.yyy

import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator


class VibratorActivity : AppCompatActivity() {
    val Context.vibrator1 :Vibrator
        get() = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vibrator)
        //从系统中获取振动器对象
        val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vibrator.vibrate(VibrationEffect.createOneShot(3000,100))
        }else{
            vibrator.vibrate(3000)
        }

    }
    fun Context.getVibrator():Vibrator{
        return getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    }
}